home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 7 / BBS in a Box - Macintosh - Volume VII (BBS in a Box) (January 1993).iso / Files / Tele / C / Comet2.1.3.cpt / include / q.h < prev    next >
Text File  |  1989-10-04  |  3KB  |  126 lines

  1. /*  Copyright 1983 by the Massachusetts Institute of Technology  */
  2.  
  3. /*
  4.     Copyright Cornell University 1986.  All rights are reserved.
  5.  
  6.     As of 4/10/86:
  7.     This source file may have no changes from the M.I.T original
  8.     other than this notice; but it has been tested as part of 
  9.     Cornell's Aztec-C port.  See notice.h
  10.  
  11. */
  12.  
  13. /*    8-13-86 John Lynn remove an extra increament of q_len in the macro q_addh */
  14. /* 10/24/86 kevin changed 0 to NULL in pointer comparisons */
  15.  
  16. /* Definitions for general-purpose queue manipulation package.
  17.    Modified from Larry Allen's queue package from CSR Unix for TCP and
  18.    tasks. */
  19.  
  20. #ifndef    _QDEF
  21.  
  22. #define _QDEF
  23.  
  24. typedef    struct    q_elt            /* queue element: cast to right type */
  25. {
  26.     struct    q_elt    *qe_next;    /* it's just a pointer to next elt */
  27. } *q_elt;
  28.  
  29. typedef    struct    queue    {        /* queue header */
  30.     q_elt    q_head;                /* first element in queue */
  31.     q_elt    q_tail;                /* last element in queue */
  32.     int    q_len;                    /* number of elements in queue */
  33.     int    q_max;                    /* maximum length */
  34.     int    q_min;                     /* minimum length */
  35. } queue;
  36.  
  37. extern    q_elt    q_deq ();
  38. extern queue *q_create();
  39.  
  40. /* The following macros implement most of the common queue operations */
  41.  
  42. /* Add an element to the head of the queue */
  43.  
  44. #define    q_addh(q, elt)     { \
  45.     if ((q)->q_head == NULL) (q)->q_tail = (elt); \
  46.     (elt)->qe_next = (q)->q_head; \
  47.     (q)->q_head = (elt); \
  48.     if(++((q)->q_len) > (q)->q_max) (q)->q_max = (q)->q_len; \
  49. }
  50.  
  51. /* Add an element to the tail of a queue */
  52.  
  53. #define    q_addt(q, elt)    { \
  54.     (elt)->qe_next = NULL; \
  55.     if ((q)->q_head == NULL) { \
  56.         (q)->q_head = (elt); \
  57.     } else { \
  58.         (q)->q_tail->qe_next = (elt); \
  59.     } \
  60.     (q)->q_tail = (elt); \
  61.     if(++((q)->q_len) > (q)->q_max) (q)->q_max = (q)->q_len; \
  62. }
  63.  
  64. /* Add an element after a specified element in the queue.  If prev == */
  65. /* &q->q_head, can be used to add an element to the head of the queue */
  66.  
  67. #define    q_adda(q, prev, new)    { \
  68.     if ((q)->q_tail == (prev) || (q)->q_tail == NULL) { \
  69.         (q)->q_tail = (new); \
  70.     } \
  71.     (new)->qe_next = (prev)->qe_next; \
  72.     (prev)->qe_next = (new); \
  73.     if(++((q)->q_len) > (q)->q_max) (q)->q_max = (q)->q_len; \
  74. }
  75.  
  76. #ifdef OLDJUNK
  77. /* Add an element before a specified element in the queue.  */
  78. /* if head is NULL or succ, just patch new elt right in 
  79.     if not, run down list to find previous elt and patch in 
  80.     requires q_elt tmpelt to be defined 
  81. */
  82.  
  83. #define    q_addb(q, succ, new)    { \
  84.     if ((q)->q_head == NULL || (q)->q_head == (succ) ) { \
  85.         (new)->qe_next = (q)->q_head; \
  86.         if ((q)->q_head == NULL) \
  87.             (q)->q_tail = (new); \
  88.         (q)->q_head = (new); \
  89.     } \
  90.     else { \
  91.         (new)->qe_next = (succ); \
  92.         tmpelt = (q)->q_head; \
  93.         while (tmpelt->qe_next != (succ)) { \
  94.             tmpelt = tmpelt->qe_next; \
  95.         } \
  96.         tmpelt->qe_next = (new); \
  97.     } \
  98.     if(++((q)->q_len) > (q)->q_max) (q)->q_max = (q)->q_len; \
  99. }
  100.  
  101. #endif
  102.  
  103. /* Delete an element from a queue, given a pointer to the preceeding element */
  104. /* Will delete the first element if prev == &q->q_head */
  105.  
  106. #define    q_dela(q, elt, prev)    { \
  107.     if ((q)->q_tail == (elt)) { \
  108.         if ((q)->q_head == (elt)) \
  109.             (q)->q_tail = NULL; \
  110.         else \
  111.             (q)->q_tail = (prev); \
  112.     } \
  113.     (prev)->qe_next = (elt)->qe_next; \
  114.     (elt)->qe_next = NULL; \
  115.     if(--((q)->q_len) < (q)->q_min) (q)->q_min = (q)->q_len; \
  116. }
  117.  
  118.  
  119. #define    aq_addt(q, elt)    {int_off(); q_addt((q), (elt)); int_on(); }
  120. #define    aq_addh(q, elt)    {int_off(); q_addh((q), (elt)); int_on(); }
  121. #define    aq_addb(q, oelt, nelt)    {int_off(); q_addb((q), (oelt), (nelt)); int_on(); }
  122.  
  123. extern q_elt aq_deq();
  124.  
  125. #endif
  126.